package com.youxin.cpcenter.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.youxin.busfoundation.bean.BaseRequestBean;
import com.youxin.busfoundation.bean.ResponseObject;
import com.youxin.busfoundation.bean.leads.RequestAssignLeadsBean;
import com.youxin.busfoundation.bean.leads.RequestLeadsBean;
import com.youxin.busfoundation.bean.leads.RequestLeadsHistoryBean;
import com.youxin.busfoundation.bean.leads.RequestLeadsPoolBean;
import com.youxin.busfoundation.bean.leads.RequestTransferLeadsBean;
import com.youxin.busfoundation.common.LoginAuth;
import com.youxin.busfoundation.bean.login.UserInfo;
import com.youxin.cpcenter.bean.MemberQueryListBean;
import com.youxin.cpcenter.common.log.LogMethod;
import com.youxin.cpcenter.model.TbEntLeadsStat;
import com.youxin.cpcenter.service.LeadsService;
import com.youxin.cpcenter.service.LeadsStatService;
import com.youxin.foundation.utils.ObjectUtils;

/**
 * @desc 线索管理接口
 * @author yingde.cao
 * @date 2018-1-5
 */
@RestController
public class LeadsServiceController extends BaseController {

	@Autowired
	private LeadsService leadsService;
	@Autowired
	private LeadsStatService leadsStatService;

	/**
	 * @desc 添加线索
	 * @param request
	 * @param httpServletResponse
	 * @param reqeustBean RequestLeadsBean
	 * @return ResponseObject
	 */
	@LogMethod(id="ent.leads.add",eventContent={})
	@RequestMapping(value = "/api/leads/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject queryCompanyInfo(HttpServletRequest request, HttpServletResponse httpServletResponse,
			@RequestBody RequestLeadsBean reqeustBean)  {	
		try {			
			ObjectUtils.requestNonNull(reqeustBean, "参数不能为空");
			ObjectUtils.requestNonEmpty(reqeustBean.getCompanyUid(), "企业标识不能为空");
			ObjectUtils.requestNonEmpty(reqeustBean.getCompanyName(), "企业全称不能为空");
			return leadsService.addLeads(reqeustBean);			
		} 
		catch (DuplicateKeyException e) {
			logger.error("/api/leads/add添加线索错误:", e);
			return ResponseObject.userFailure("当前线索已经添加到线程池");
		}
		catch (Exception e) {
			logger.error("/api/leads/add异常", e);
			return ResponseObject.systemError(e);
		}
	}

	/**
	 * 公司线索列表
	 * @param request
	 * @param httpServletResponse
	 * @param reqeustBean
	 * @return
	 */
	@LogMethod(id="ent.company.list",eventContent={})
	@RequestMapping(value = "/api/leads/company/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject listComanyLeads(HttpServletRequest request, HttpServletResponse httpServletResponse,
			@RequestBody RequestLeadsPoolBean reqeustBean)  {		
		try {			
			ObjectUtils.requestNonNull(reqeustBean, "参数不能为空");
			return leadsService.listCompanyLeadsPool(reqeustBean);	
		}
		catch (Exception e) {
			logger.error("/api/leads/add异常", e);
			return ResponseObject.systemError(e);
		}
	}
	
	@LogMethod(id="ent.leads.stat",eventContent={})
	@RequestMapping(value = "/api/leads/stat", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject listLeadsStat(HttpServletRequest request, HttpServletResponse httpServletResponse,
			@RequestBody BaseRequestBean reqeustBean)  {		
		try {			
			ObjectUtils.requestNonNull(reqeustBean, "参数不能为空");
			UserInfo userInfo=LoginAuth.getUserInfo();
			TbEntLeadsStat stat=leadsStatService.getTbEntLeadsStat(userInfo.getEntIdentifyId());
			return new ResponseObject(stat);
		}
		catch (Exception e) {
			logger.error("/api/leads/add异常", e);
			return ResponseObject.systemError(e);
		}
	}
	
	/**
	 * @desc 个人线索列表
	 * @param request
	 * @param httpServletResponse
	 * @param reqeustBean
	 * @return
	 */
	@LogMethod(id="ent.leads.person.list",eventContent={})
	@RequestMapping(value = "/api/leads/person/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject listPersonLeads(HttpServletRequest request, HttpServletResponse httpServletResponse,
			@RequestBody RequestLeadsPoolBean reqeustBean)  {		
		try {			
			ObjectUtils.requestNonNull(reqeustBean, "参数不能为空");
			return leadsService.listPersonalLeadsPool(reqeustBean);		
		}
		catch (Exception e) {
			logger.error("/api/leads/person/list异常", e);
			return ResponseObject.systemError(e);
		}
	}

	/**
	 * @desc 分配线索
	 * @param request
	 * @param httpServletResponse
	 * @param reqeustBean
	 * @return
	 */
	@LogMethod(id="ent.leads.company.assign",eventContent={})
	@RequestMapping(value = "/api/leads/company/assign", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject assignLeads(HttpServletRequest request, HttpServletResponse httpServletResponse,
			@RequestBody RequestAssignLeadsBean reqeustBean)  {		
		try {			
			ObjectUtils.requestNonNull(reqeustBean, "参数不能为空");
			return leadsService.assignLeads(reqeustBean);		
		}
		catch (DuplicateKeyException e) {
			logger.error("/api/leads/company/assign错误:", e);
			return ResponseObject.userFailure("当前线索已经分配");
		}
		catch (Exception e) {
			logger.error("/api/leads/company/assign异常", e);
			return ResponseObject.systemError(e);
		}
	}
	
	/**
	 * @desc 认领线索
	 * @param request
	 * @param httpServletResponse
	 * @param reqeustBean AssignLeadsBean 参数
	 * @return ResponseObject
	 */
	@LogMethod(id="ent.leads.company.request",eventContent={})
	@RequestMapping(value = "/api/leads/company/request", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject requestLeads(HttpServletRequest request, HttpServletResponse httpServletResponse,
			@RequestBody RequestAssignLeadsBean reqeustBean)  {		
		try {			
			ObjectUtils.requestNonNull(reqeustBean, "参数不能为空");
			return leadsService.requestLeads(reqeustBean);		
		}catch (DuplicateKeyException e) {
			logger.error("/api/leads/company/request错误:", e);
			return ResponseObject.userFailure("当前线索已经分配");
		}
		catch (Exception e) {
			logger.error("/api/leads/company/request异常", e);
			return ResponseObject.systemError(e);
		}
	}
	
	/**
	 * @desc 释放线索
	 * @param request HttpServletRequest请求
	 * @param httpServletResponse HttpServletResponse输出
	 * @param reqeustBean AssignLeadsBean 参数
	 * @return ResponseObject
	 */
	@LogMethod(id="ent.leads.release",eventContent={})
	@RequestMapping(value = "/api/leads/release", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject releaseLeads(HttpServletRequest request, HttpServletResponse httpServletResponse,
			@RequestBody RequestAssignLeadsBean reqeustBean)  {		
		try {			
			ObjectUtils.requestNonNull(reqeustBean, "参数不能为空");
			return leadsService.releaseLeads(reqeustBean);		
		}
		catch (Exception e) {
			logger.error("/api/leads/release异常", e);
			return ResponseObject.systemError(e);
		}
	}
	
	/**
	 * @desc 更新线索状态
	 * @param request HttpServletRequest请求
	 * @param httpServletResponse HttpServletResponse输出
	 * @param reqeustBean AssignLeadsBean 参数
	 * @return ResponseObject
	 */
	@LogMethod(id="ent.leads.change.status",eventContent={})
	@RequestMapping(value = "/api/leads/change/status", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject changeLeadsStatus(HttpServletRequest request, HttpServletResponse httpServletResponse,
			@RequestBody RequestAssignLeadsBean reqeustBean)  {		
		try {			
			ObjectUtils.requestNonNull(reqeustBean, "参数不能为空");
			return leadsService.changeLeadsStatus(reqeustBean);		
		}
		catch (Exception e) {
			logger.error("/api/leads/change/status异常", e);
			return ResponseObject.systemError(e);
		}
	}
	
	/**
	 * @desc 流转线索状态
	 * @param request HttpServletRequest请求
	 * @param httpServletResponse HttpServletResponse输出
	 * @param reqeustBean AssignLeadsBean 参数
	 * @return ResponseObject
	 */
	@LogMethod(id="ent.leads.transfer",eventContent={})
	@RequestMapping(value = "/api/leads/transfer", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject transferLeads(HttpServletRequest request, HttpServletResponse httpServletResponse,
			@RequestBody RequestTransferLeadsBean reqeustBean)  {		
		try {			
			ObjectUtils.requestNonNull(reqeustBean, "参数不能为空");
			ObjectUtils.requestNonNull(reqeustBean.getDescription(), "说明不能为空");
			ObjectUtils.reququestCondition(reqeustBean.getDescription().length()<2000, "说明不能超过2000字");
			return leadsService.transferLeads(reqeustBean);		
		}
		catch (Exception e) {
			logger.error("/api/leads/transfer异常", e);
			return ResponseObject.systemError(e);
		}
	}
	
	@LogMethod(id="ent.leads.history",eventContent={})
	@RequestMapping(value = "/api/leads/history", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject leadsHistory(HttpServletRequest request, HttpServletResponse httpServletResponse,
			@RequestBody RequestLeadsHistoryBean reqeustBean)  {		
		try {			
			ObjectUtils.requestNonNull(reqeustBean, "参数不能为空");			
			return leadsService.leadsHistory(reqeustBean);		
		}
		catch (Exception e) {
			logger.error("/api/leads/history异常", e);
			return ResponseObject.systemError(e);
		}
	}

	/**
	 * @desc 删除公司线索
	 * @param request HttpServletRequest请求
	 * @param httpServletResponse HttpServletResponse输出
	 * @param reqeustBean AssignLeadsBean 参数
	 * @return ResponseObject
	 */
	@LogMethod(id="ent.leads.delete",eventContent={})
	@RequestMapping(value = "/api/leads/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject deleteLeads(HttpServletRequest request, HttpServletResponse httpServletResponse,
			@RequestBody RequestAssignLeadsBean reqeustBean)  {		
		try {			
			ObjectUtils.requestNonNull(reqeustBean, "参数不能为空");
			return leadsService.deleteLeads(reqeustBean);		
		}
		catch (Exception e) {
			logger.error("/api/leads/transfer异常", e);
			return ResponseObject.systemError(e);
		}
	}
	
	@LogMethod(id="ent.leads.follow",eventContent={})
	@RequestMapping(value = "/api/leads/follow", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject leadsFollow(HttpServletRequest request, HttpServletResponse httpServletResponse,
			@RequestBody MemberQueryListBean reqeustBean)  {		
		try {			
			ObjectUtils.requestNonNull(reqeustBean, "参数不能为空");
			return leadsService.followList(reqeustBean);		
		}
		catch (Exception e) {
			logger.error("/api/leads/transfer异常", e);
			return ResponseObject.systemError(e);
		}
	}

}
